package com.tongzhan.controller;

import com.tongzhan.base.BaseController;
import com.tongzhan.base.Return;
import com.tongzhan.entity.party.ActionCenter;
import com.tongzhan.param.ActionParam;
import com.tongzhan.utils.SnowFlakeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;

/**
 * 活动
 *
 * @author hongjianbin
 * @date 2020/7/20
 */
@RequestMapping("/action")
@RestController
public class ActionController extends BaseController {

    /**
     * 添加
     * @param param
     * @return
     * @throws ParseException
     */
    @PostMapping("/addAction")
    public Return addAction(@RequestBody ActionParam param) {
        if (param.getAction_content().trim().length() > 200) {
            return Return.getErrorResult("活动内容过长");
        }
        if (param.getActionTypeId()==null) {
            return Return.getErrorResult("活动类型不能为空");
        }
        if (param.getAction_address().trim().length() > 50) {
            return Return.getErrorResult("地点输入过长");
        }
        ActionCenter center = new ActionCenter();
        BeanUtils.copyProperties(param,center);
        SnowFlakeUtils snowFlake = new SnowFlakeUtils(3, 3);
        center.setAid(String.valueOf(snowFlake.nextId()));
        center.setAdd_userid(param.getUserId());
        center.setAction_picture(param.getAction_picture());
        center.setAction_content(param.getAction_content());
        center.setJoin_count(param.getJoin_count());
        center.setAction_address(param.getAction_address());
        center.setTime(param.getTime());
        center.setActionTypeId(param.getActionTypeId());

        long l = actionService.addAction(center);
        if (l > 0) {
            return Return.getSuccessResult("新增成功!");
        }
        return Return.getErrorResult("新增失败!");
    }

    /**
     * 活动详情
     * @param aid
     * @return
     */
    @PostMapping("/actionDetail")
    public Return getDetail(String aid,String userId) {
        if (aid == null) {
            return Return.getErrorResult("缺少id参数!");
        }
        ActionCenter actionCenter = actionService.actionDetail(aid,userId);
        return Return.getSuccessResult(actionCenter);
    }

    /**
     * 活动列表
     *
     * @param page
     * @param size
     * @param userId 用户id
     * @param auditStatus 审核状态(0: 未审核、1：通过，2：不通过)
     * @return
     */
    @PostMapping("/actionList")
    public Return actionList(Integer page, Integer size, String userId,Integer auditStatus) {
        page = page == null ? 0 : page - 1;
        List list = actionService.actionList(page, size,userId, auditStatus);
        return Return.getSuccessResult(list);
    }


    /**
     * 审核
     * @param id
     * @param type 审核状态(0: 未审核、1：通过，2：不通过)
     * @param userId 审核人id
     * @return
     */
    @PostMapping("/auditAction")
    public Return auditAction(String id, Integer type, String userId) {
        if (StringUtils.isEmpty(id) || type ==null) {
            return  Return.getErrorResult("参数为空");
        }
        long flag = actionService.auditAction(id, type, userId);
        if (flag > 0) {
            return Return.getSuccessResult("审核成功!");
        }
        return Return.getErrorResult("审核失败!");
    }
}
